<#function dashedToCamel(s)>
    <#return s
    ?replace('(^_+)|(_+$)', '', 'r')
    ?replace('\\_+(\\w)?', ' $1', 'r')
    ?replace('([A-Z])', ' $1', 'r')
    ?capitalize
    ?replace(' ' , '')
    ?uncap_first
    >
</#function>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="${cfg.package_name}.repository.mapper.${entity}Mapper">

<#if enableCache>
    <!-- 开启二级缓存 -->
    <cache type="org.mybatis.caches.ehcache.LoggingEhcache"/>
</#if>
    <resultMap id="BaseResultMap" type="${cfg.package_name}.model.bean.${entity}" >
        <id column="${cfg.primaryKey}" property="${cfg.primaryKey}"/>
        <#list cfg.columnList as column>
        <#if column.isPrimaryKey=="false" && (column.tableAliasName=="" || column.tableAliasName=="t1")>
        <result column="${column.columnName}" property="${column.propertyName}" />
        </#if>
        </#list>

        <#if cfg.hasM2O>
        <!--
            Many to One: 实时加载
            association：用于映射关联查询单个对象的信息
            property：要讲关联查询的用户信息映射到订单pojo列中的哪个属性里
            javaType:表示关联查询的对象类型（可以使用别名）
         -->
        <association property="${cfg.classNameFirstWordLowerM2O}" javaType="${cfg.package_name}.model.bean.${cfg.classNameFirstWordUpperM2O}">
            <id column="${cfg.tableNameFirstWordLowerM2O}_id" property="${cfg.primaryKey}"/>
            <#list cfg.columnList as column>
            <#if column.columnNameSql!="t2.id" && column.tableAliasName=="t2">
            <result column="${column.columnName}" property="${dashedToCamel(column.columnNameSql?substring(3))}" />
            </#if>
            </#list>
        </association>
        </#if>

    </resultMap>

    <!-- One to Many ：延迟加载信息列表-->
    <!-- 使用One to Many集合时，select 中 resultMap="BaseResultMapO2M" -->
    <resultMap id="BaseResultMapO2M" type="${cfg.package_name}.model.bean.${entity}" extends="BaseResultMap">
        <!--
            One to Many ：延迟加载信息列表
            select:指定延迟加载需要执行的sql配置id， 调用外部方法: 对应xml的namespace + 对应xml中的代码片段的id,
            column:column作为select语句的参数传入,如果只传一个参数id可以简写: column="id"
                   传多值：column="{qid=id,sort=sort}" qid/sort是定义的变量名, id/sort是主表的字段id/sort
            <collection property="a2userList" column="id"
                        ofType="${cfg.package_name}.model.bean.A2user"
                        select="${cfg.package_name}.repository.mapper.A2userMapper.mySelectByA2deptId" />
        -->
        <#list cfg.o2mList as o2m>
        <#if o2m.hasO2M>
        <collection
            fetchType="lazy"
            property="${o2m.classNameFirstWordLowerO2M}List"
            column="${o2m.column}"
            ofType="${cfg.package_name}.model.bean.${o2m.classNameFirstWordUpperO2M}"
            select="mySelect${o2m.classNameFirstWordUpperO2M}ListBy" />
        </#if>
        </#list>
    </resultMap>

    <sql id="Base_Column_List" >
        ${cfg.columnStr?replace(",",",\n        ")}
    </sql>

    <!--
    这个mapper.xml继承了base/mapper.xml;
    1, 这里写的statement, 与base/mapper.xml中同名时，将覆盖base中的方法。
    2, resultMap 和 sql 没有继承特性，如有变更，需指向各自的resultMap和sql。
    -->

</mapper>
